package com.jerome.fs.server;

import com.jerome.fs.constants.AppConstant;
import com.jerome.fs.entities.JsonVo;
import com.jerome.fs.entities.WeatherVo;
import com.jerome.fs.http.HttpStatus;
import com.jerome.fs.http.Response;
import com.jerome.fs.http.client.HttpClient;
import com.jerome.fs.lamport.LamportClock;
import com.jerome.fs.util.JsonUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Content Server
 *
 * @author Jerome
 * @date 2023/7/31
 */
public class ContentServer {
    /**
     * local lamportClock
     */
    private static LamportClock lamportClock = new LamportClock(0);

    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("Please provide some command-line arguments. \nTips:java -cp work2.jar com.jerome.fs.server.ContentServer  127.0.0.1:4567 ");
        } else {
            String url = args[0];
            if (!url.startsWith(AppConstant.HTTP_PREFIX)) {
                url = AppConstant.HTTP_PREFIX + url;
            }
            System.out.println("Enter 'q' to exit");
            System.out.println("Please enter the address of your local weather file");
            Scanner scan = new Scanner(System.in);
            while (true) {
                try {
                    String in = scan.nextLine();
                    if ("q".equals(in)){
                        break;
                    }
                    File file = new File(in);
                    if (!file.exists()) {
                        System.out.println(in + " file does not exist");
                        continue;
                    }
                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(new FileInputStream(file)));
                    Map<String, String> map = new HashMap<>();
                    String line;
                    String[] split;
                    while ((line = bufferedReader.readLine()) != null) {
                        split = line.split(":");
                        map.put(split[0], split.length > 1 ? split[1] : "");
                    }
                    JsonVo jsonVo = new JsonVo(lamportClock.tick(lamportClock.getLatestTime()));
                    jsonVo.putAll(map);
                    Map<String, String> headers = new HashMap<>();
                    headers.put(AppConstant.LAMPORT_HEADER_NAME, lamportClock.getLatestTime() + "");
                    Response response = HttpClient.put(url+"/weather", null, headers, JsonUtil.writeValueAsString(jsonVo));
                    if (response.getCode() < HttpStatus.HTTP_BAD_REQUEST) {
                        WeatherVo vo = JsonUtil.readValue(response.getMessage(), WeatherVo.class);
                        //更新时钟
                        lamportClock.tick(vo.getLatestTime());
                    }
                    System.out.println(response);
                } catch (FileNotFoundException e) {
                    System.out.println("File Not Found:" + args[1]);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("ContentServer quit");
        }
    }
}
